package org.liangsy.rest.user.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyTestDemo1 {

    private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";
    private static final int TOTAL_INSERTS = 10000000; // 总共要插入的数据量
    private static final int THREAD_COUNT = 10; // 线程数量
    private static final long START_VALUE = 9885688; // 起始值

    public static void main(String[] args) {
        // 记录并打印开始时间
        LocalDateTime startDateTime = LocalDateTime.now();
        System.out.println("Start Time: " + startDateTime);

        long startTimeMillis = System.currentTimeMillis();

        ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT);

        int insertsPerThread = TOTAL_INSERTS / THREAD_COUNT;

        for (int i = 0; i < THREAD_COUNT; i++) {
            long threadStartValue = START_VALUE + (i * insertsPerThread);
            long threadEndValue = (i == THREAD_COUNT - 1) ? START_VALUE + TOTAL_INSERTS : threadStartValue + insertsPerThread;
            executorService.submit(new InsertTask(threadStartValue, threadEndValue));
        }

        executorService.shutdown();

        // 记录并打印结束时间以及总耗时
        long endTimeMillis = System.currentTimeMillis();
        System.out.println("Total execution time: " + (endTimeMillis - startTimeMillis) + " ms");
        System.out.println("End Time: " + LocalDateTime.now());
    }

    private static class InsertTask implements Runnable {
        private final long startValue;
        private final long endValue;

        public InsertTask(long startValue, long endValue) {
            this.startValue = startValue;
            this.endValue = endValue;
        }

        @Override
        public void run() {
            Connection connection = null;
            PreparedStatement preparedStatement = null;

            try {
                // Establish the connection.
                connection = DriverManager.getConnection(URL, USER, PASSWORD);

                // SQL insert query. Adjust column names according to your table structure.
                String query = "INSERT INTO test_table (test_name, tast_key1, tast_key2, tast_key3) VALUES (?, ?, ?, ?)";
                preparedStatement = connection.prepareStatement(query);

                // Loop to insert data.
                for (long i = startValue; i < endValue; i++) {
                    String value = String.valueOf(i);
                    preparedStatement.setString(1, value); // test_name
                    preparedStatement.setString(2, value); // tast_key1
                    preparedStatement.setString(3, value); // tast_key2
                    preparedStatement.setString(4, value); // tast_key3

                    preparedStatement.addBatch();

                    // Execute batch every 1000 inserts to reduce memory usage.
                    if ((i - startValue + 1) % 1000 == 0) {
                        preparedStatement.executeBatch();
                    }
                }

                // Insert any remaining records.
                preparedStatement.executeBatch();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (preparedStatement != null) preparedStatement.close();
                    if (connection != null) connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
